package org.transfer.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.InetAddress;
import java.util.Properties;

import org.apache.log4j.PropertyConfigurator;

public class ConfigUtil {
	private static Properties configure;

	public static void init(String fileName) {
		FileInputStream fis = null;
		try {
			File configFile = new File(fileName);
			Properties p = new Properties();
			fis = new FileInputStream(configFile);
			p.load(fis);
			init(p);
		} catch (FileNotFoundException e) {
			System.err.println("Log config file [" + fileName + "] is not exist!");
		} catch (IOException e) {
			System.err.println("Log config file [" + fileName + "] can't be opened!");
		} finally {
			try {
				if (null != fis) {
					fis.close();
				}
			} catch (IOException e) {
			}
		}
	}

	public static void init(Properties p) {
		configure = p;
		PropertyConfigurator.configure(configure);
	}

	public static Properties getConfigure() {
		return configure;
	}

	public String getPath() {
		return this.getClass().getResource("/").getPath();
	}

	/**
	 * 获取本机ip
	 * 
	 * @return
	 */
	public static String getHostIp() {
		InetAddress ia = null;
		try {
			ia = ia.getLocalHost();
			System.out.println("本机的ip是 ：" + ia.getHostAddress());
			return ia.getHostAddress();
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

}
